﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;

namespace Shared
{
    public static class Helper
    {
        public static string RequestHtml(string url, int retry, int timeout, IWebProxy proxy)
        {
            for (int i = 0; i <= retry; i++)
            {
                try
                {
                    HttpWebRequest req = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
                    req.KeepAlive = false;
                    req.Timeout = timeout * 1000;
                    if (proxy != null)
                        req.Proxy = proxy;

                    HttpWebResponse response = null;
                    StreamReader resStream = null;
                    try
                    {
                        response = (HttpWebResponse)req.GetResponse();
                        resStream = new StreamReader(response.GetResponseStream(), Encoding.UTF8);

                    }
                    catch (ArgumentOutOfRangeException)
                    {
                        continue;
                    }

                    return resStream.ReadToEnd();
                }

                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return String.Empty;
                }
            }

            return String.Empty;
        }

        public static string RequestHtmlWithCookie(string url, int retry, int timeout, IWebProxy proxy, out CookieContainer cookie)
        {
            for (int i = 0; i <= retry; i++)
            {
                try
                {
                    HttpWebRequest req = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
                    req.CookieContainer = new CookieContainer();
                    req.KeepAlive = false;
                    req.Timeout = timeout * 1000;
                    req.UserAgent = "Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";

                    if (proxy != null)
                        req.Proxy = proxy;

                    HttpWebResponse response = null;
                    StreamReader resStream = null;
                    try
                    {
                        response = (HttpWebResponse)req.GetResponse();
                        resStream = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    }
                    catch (ArgumentOutOfRangeException)
                    {
                        continue;
                    }

                    cookie = req.CookieContainer;
                    return resStream.ReadToEnd();
                }

                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    cookie = null;
                    return String.Empty;
                }
            }

            cookie = null;
            return String.Empty;
        }

        public static string PostData(string url, string data, int retry, int timeout, IWebProxy proxy, Encoding enc, CookieContainer cookie)
        {
            for (int i = 0; i <= retry; i++)
            {
                try
                {
                    HttpWebRequest req = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
                    req.KeepAlive = true;
                    req.Timeout = timeout * 1000;
                    req.Method = "POST";
                    req.CookieContainer = cookie;
                    req.AllowAutoRedirect = false;
                    req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                    req.Referer = "http://proxybonanza.com/";


                    if (proxy != null)
                        req.Proxy = proxy;

                    byte[] byteArray = enc.GetBytes(data);
                    req.ContentType = "application/x-www-form-urlencoded";
                    req.ContentLength = byteArray.Length;
                    req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.11) Gecko/20100701 Firefox/3.5.11 (.NET CLR 3.5.30729)";

                    HttpWebResponse response = null;

                    StreamReader resStream = null;
                    try
                    {
                        response = (HttpWebResponse)req.GetResponse();
                        resStream = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    }
                    catch (ArgumentOutOfRangeException)
                    {
                        continue;
                    }

                    return resStream.ReadToEnd();
                }

                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return String.Empty;
                }
            }

            return String.Empty;

        }

        public static string ToReport<T>(this T instance)
        {
            string result = String.Empty;

            Type type = typeof(T);

            foreach (PropertyInfo info in type.GetProperties())
            {
                if (info.GetCustomAttributes(typeof(ReportAttribute), true).Length > 0)
                {
                    result += info.Name + Environment.NewLine;
                }
            }

            return result;
        }

        public static string ToReport(this List<string> value)
        {
            string result = String.Empty;

            foreach (var item in value)
            {
                result += item + Environment.NewLine;
            }

            return result + Environment.NewLine;
        }

        public static T FromBase64StringToObject<T>(string base64)
        {
            byte[] buffer = Convert.FromBase64String(base64);

            DataContractSerializer ds = new DataContractSerializer(typeof(T));

            using (MemoryStream m = new MemoryStream(buffer))
            {
                T data = (T)ds.ReadObject(m);

                return data;
            }


        }
    }
}
